import {isNumber } from 'lodash'
// 获取节点和线的异常提示信息
export const getExceptionInfo = (data) => {
  const messages = [];
  const {
    responseTimeDodRate,
    responseTimeRingRate,
    requestTotalDodRate,
    requestTotalRingRate,
    errorRateDodRate,
    errorRateRingRate,
    errorRate,
    responseTime,
    exceptionMsg,
    errorMessage,
  } = data;
  const alarmMessages = errorMessage || [];

  // 若后台返回了同环比异常信息，则使用后台的异常信息
  if (exceptionMsg?.length) {
    return { exceptionMsg, alarmMessages };
  }

  if (isNumber(responseTimeDodRate) && responseTimeDodRate > 50) {
    messages.push(`响应时间同比增加${+responseTimeDodRate.toFixed(2)}%`);
  }
  if (isNumber(responseTimeRingRate) && responseTimeRingRate > 50) {
    messages.push(`响应时间环比增加${+responseTimeRingRate.toFixed(2)}%`);
  }
  if (isNumber(requestTotalDodRate) && requestTotalDodRate > 50) {
    messages.push(`请求量同比增加${+requestTotalDodRate.toFixed(2)}%`);
  }
  if (isNumber(requestTotalRingRate) && requestTotalRingRate > 50) {
    messages.push(`请求量环比增加${+requestTotalRingRate.toFixed(2)}%`);
  }
  if (isNumber(errorRateDodRate) && errorRateDodRate > 50) {
    messages.push(`错误率同比增加${+errorRateDodRate.toFixed(2)}%`);
  }
  if (isNumber(errorRateRingRate) && errorRateRingRate > 50) {
    messages.push(`错误率环比增加${+errorRateRingRate.toFixed(2)}%`);
  }
  if (isNumber(errorRate) && errorRate > 50) {
    messages.push(`错误率${+errorRate.toFixed(2)}%`);
  }
  if (isNumber(responseTime) && responseTime > 5000000000) {
    messages.push(`响应时间${formatUnit(responseTime, "ns")}`);
  }

  return { exceptionMsg: messages, alarmMessages };
};
